package com.song.EMS.controller;

import com.song.EMS.bean.Courier;
import com.song.EMS.bean.Message;
import com.song.EMS.bean.ResultData;
import com.song.EMS.mvc.ResponseBody;
import com.song.EMS.service.impl.CourierServiceImpl;
import com.song.EMS.service.impl.ExpressServiceImpl;
import com.song.EMS.service.impl.UserServiceImpl;
import com.song.EMS.utils.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class CourierController {
    private CourierServiceImpl courierService = new CourierServiceImpl();

    /**
     * 查询快递员所有信息
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/courier/select.do")
    public String selectAll(HttpServletRequest request, HttpServletResponse response) {
        String pageIndex = request.getParameter("pageIndex");
        String pageSize = request.getParameter("pageSize");
        List<Courier> couriers = courierService.selectCouriers(true, Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        //获取总信息数
        int total = courierService.selectCouriers(false, null, null).size();
        ResultData<Courier> data = new ResultData<>();
        data.setRows(couriers);
        data.setTotal(total);
        return JsonUtil.toJSON(data);
    }

    /**
     * 新增快递员
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/courier/insert.do")
    public String insertCourier(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        //获取前端数据
        String courierName = request.getParameter("courierName");
        String password = request.getParameter("password");
        String IDCard = request.getParameter("IDCard");
        String courierPhone = request.getParameter("courierPhone");
        String numberOfDelivery = request.getParameter("numberOfDelivery");
        Courier courier = new Courier(courierName,password,IDCard,courierPhone,Integer.parseInt(numberOfDelivery));
        //调用service
        Integer integer = courierService.insertCourier(courier);
        if (integer == 1){
            data = new Message(1,"新增成功");
        }else if (integer == -1){
            data = new Message(-1,"电话号码重复");
        }else if (integer == -2){
            data = new Message(-2,"身份证明已被注册");
        }else{
            data = new Message(0,"新增失败");
        }
        return JsonUtil.toJSON(data);

    }

    /**
     * 通过手机号查询快递员信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/courier/selectByPhone.do")
    public String selectByPhone(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        //获取前端数据
        String courierPhone = request.getParameter("courierPhone");

        //调用service
        Courier courier = courierService.selectByPhone(courierPhone);

        if (courier == null){
            data = new Message(0,"该号码不存在");
        }else{
            data = new Message(1,"查询成功",courier);
        }

        return JsonUtil.toJSON(data);
    }

    /**
     * 修改快递员信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/courier/update.do")
    public String updateCourier(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        //获取前端数据
        String courierId = request.getParameter("courierId");
        String courierName = request.getParameter("courierName");
        String password = request.getParameter("password");
        String IDCard = request.getParameter("IDCard");
        String courierPhone = request.getParameter("courierPhone");
        String numberOfDelivery = request.getParameter("numberOfDelivery");
        Courier courier = new Courier(Integer.parseInt(courierId),courierName,password,IDCard,courierPhone,Integer.parseInt(numberOfDelivery));
        System.out.println(courier);
        //调用service
        Integer integer = courierService.updateCourier(courier);
        if (integer == 1){
            data = new Message(1,"修改成功");
        }else if (integer == -1){
            data = new Message(-1,"电话号码重复");
        }else if (integer == -2){
            data = new Message(-2,"身份证明已被注册");
        }else{
            data = new Message(0,"新增失败");
        }
        System.out.println(data.getResult());
        return JsonUtil.toJSON(data);
    }

    /**
     * 删除快递员
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/courier/delete.do")
    public String deleteCourier(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        String courierId = request.getParameter("courierId");
        System.out.println(courierId);
        Boolean aBoolean = courierService.deleteCourier(Integer.parseInt(courierId));
        if (aBoolean){
            data = new Message(1,"删除成功");
        }else{
            data = new Message(0,"删除失败");
        }
        return JsonUtil.toJSON(data);
    }

    /**
     * 控制台中的数据
     * @param request
     * @param response
     * @return 给前端的用户数据
     */
    @ResponseBody("/admin/views/courier-console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        //调用service
        Message data = null;
        List courierList = courierService.courierConsole();
        if(courierList.size() >0){
            data = new Message(1,null,courierList);

        }else {
            data = new Message(-1,null);
        }

        return JsonUtil.toJSON(data);
    }
}
